
source('utils.R')
source('read_dataset.R')

## inflation adjustment
cols = c('r_val','d_val','r_val_pcc_only','d_val_pcc_only')
df12[, (cols) := lapply(.SD, \(x) x * 1.12), .SDcols = cols]
df16[, (cols) := lapply(.SD, \(x) x * 1.037037), .SDcols = cols]

cdf = list(
  d12 = df12[d_val>0, .(nat_quant_fine, val = d_val)][,nq_camp:=make_bins(val,c(0,.9,.99, 1))],
  r12 = df12[r_val>0, .(nat_quant_fine, val = r_val)][,nq_camp:=make_bins(val,c(0,.9,.99, 1))],
  d16 = df16[d_val>0, .(nat_quant_fine, val = d_val)][,nq_camp:=make_bins(val,c(0,.9,.99, 1))],
  r16 = df16[r_val>0, .(nat_quant_fine, val = r_val)][,nq_camp:=make_bins(val,c(0,.9,.99, 1))],
  d20 = df20[d_val>0, .(nat_quant_fine, val = d_val)][,nq_camp:=make_bins(val,c(0,.9,.99, 1))],
  r20 = df20[r_val>0, .(nat_quant_fine, val = r_val)][,nq_camp:=make_bins(val,c(0,.9,.99, 1))]
)

# for each campaign, get total from each bin
cdf = lapply(cdf, function(x) x[,.(money = sum(val,na.rm=T)),nat_quant_fine])
# concatenate, with candidate ID column
cdf = rbindlist(cdf, use.names=T, idcol = 'cand')

cdf[,nat_quant_fine:=as.numeric(as.character(nat_quant_fine))]

cdf[,nq_bin:=fcase(nat_quant_fine < .9, '[0,90)',
                   nat_quant_fine < .99, '[90,99)',
                   nat_quant_fine < .999, '[99,99.9)',
                   nat_quant_fine < 1.01, '[99.9,)',
                   default = NA_character_)]

cdf = cdf[,.(tot=sum(money,na.rm=T)),.(cand,nq_bin)]
cdf[,pct:=tot/sum(tot,na.rm=T),cand]
cdf[,cand_print:=fcase(cand=='d12','Obama \'12',
                       cand=='d16','Clinton \'16',
                       cand=='d20','Biden \'20',
                       cand=='r12','Romney \'12',
                       cand=='r16','Trump \'16',
                       cand=='r20','Trump \'20') %>%
      factor(levels = c( 'Romney \'12', 'Trump \'16', 'Trump \'20', 'Obama \'12', 'Clinton \'16', 'Biden \'20'))]


cdf = cdf %>% group_by(cand_print) %>%
  arrange(desc(factor(nq_bin,levels=c('[0,90)','[90,99)','[99,99.9)','[99.9,)'))),.by_group = T) %>%
  mutate(pct_lab = cumsum(pct) - pct/2)

# Clean and prepare the data
cdf <- cdf %>%
  mutate(
    Candidate_Year = cand_print %>%
      forcats::fct_relevel("Romney '12", "Trump '16", "Trump '20", "Obama '12", "Clinton '16", "Biden '20"),
    Percent3 = (pct * 100) %>% round(1),
    Percent2 = (pct * 100) %>% round(0),
    Wealth_Rank = nq_bin,
    Amount = (tot / 1000000) %>% round(0),
    Party = case_when(
      str_detect(Candidate_Year, "Romney|Trump") ~ "Republican", 
      str_detect(Candidate_Year, "Obama|Clinton|Biden") ~ "Democratic" 
    )
  )

save(cdf, file = 'summary_data/fig2.rda')

comp <- cdf %>%
  ungroup() %>%
  filter(str_detect(Candidate_Year, "Romney|Trump")) %>%
  mutate(Year = paste0("20", str_sub(cand_print, -2, -1L)),
         Candidate = case_when(
           str_detect(Candidate_Year, "Romney") ~ "Romney",
           str_detect(Candidate_Year, "Trump")  ~ "Trump"
         )  
  ) %>% 
  select(Candidate, Year, Wealth_Rank, Amount) %>%
  pivot_wider(names_from = c(Candidate, Year), values_from = Amount) %>%
  mutate(
    Change_2016 = (Trump_2016 - Romney_2012) / Romney_2012 * 100,
    Change_2020 = (Trump_2020 - Romney_2012) / Romney_2012 * 100
  ) %>%
  select(Wealth_Rank, Change_2016, Change_2020) %>%
  pivot_longer(cols = c(Change_2016, Change_2020), names_to = "Year", values_to = "Percent_Change")

save(comp, file = 'summary_data/fig3.rda')
